#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int g_data = 22;
pthread_key_t key;


void destroy( int t )
{
	printf("thread id %u\n", pthread_self());
}

void *pthread1( void *arg )
{

	int data = (int)pthread_setspecific( key, ( void * )g_data );	
	
	printf( "data [%d].\n", data );


	printf( "%s data[ %d ].\n", __FUNCTION__, pthread_getspecific( key ) );
  
	sleep(1);


}

void * pthread2( void *arg )
{
	//int data = (int )pthread_getspecific( key );

	printf( "%s data[ %d ].\n", __FUNCTION__, pthread_getspecific( key ) );

	return;

}


int main( void )
{
	pthread_t tid1, tid2;
	int ret = 0;

	ret = pthread_key_create( &key, destroy );
	if ( ret != 0 ) {
		printf( "Pthread_key_create failed.\n" );
		return -1;
	}
	pthread_create( &tid1, NULL, pthread1, NULL );
	pthread_create( &tid2, NULL, pthread2, NULL );


	pthread_join( tid1, NULL );
	pthread_join( tid2, NULL );


	return 0;
}
